package com.example.asus.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnStart;
    private Button btnStop;
    private Button btnBind;
    private Button btnUnBind;
    private ServiceConnection serviceConnection;
    private MyBinder myBinder;

    private Button btnAdd;
    private Button btnUpper;
    private Button btnCurTime;
    private Button btnOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.start_service);
        btnStop = findViewById(R.id.stop_service);
        btnBind = findViewById(R.id.bind_service);
        btnUnBind = findViewById(R.id.unbind_service);
        btnAdd =  findViewById(R.id.btn_add);

        btnUpper=findViewById(R.id.btn_toUpperCase);
        btnCurTime=findViewById(R.id.btn_curTime);
        btnOrder=findViewById(R.id.btn_reverse_order);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity", "onServiceConnected");
                myBinder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity", "onServiceDisconnected");
            }

            @Override
            public void onBindingDied(ComponentName name) {
                Log.e("MainActivity", "onBindingDied");
            }
        };
        //大写
        btnUpper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=myBinder.toUpperCase("hello");
                Log.e("myBinder.toUpperCase","str = "+str);
            }
        });
        //获取当前时间
        btnCurTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time = myBinder.getTimeStr();
               Log.e("myBinder.getTimeStr","time = "+time);
            }
        });
        //字符串逆序
        btnOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=myBinder.strReverse("I Love Android");
                Log.e("myBinder.strReverse","str = "+str);
            }
        });
//绑定
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });
//取消绑定
        btnUnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });
//计算和
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               int result = myBinder.add(100, 200);
               Toast.makeText(MainActivity.this, "result = " + result, Toast.LENGTH_SHORT).show();

            }

        });

//启动服务
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
//停止服务
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                stopService(intent);
            }
        });
    }
}

